package com.gupao.alg.alg0;

public class Topic2_2_IsPalindromic {
    static ListNode temp;

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4,5,6,7,8};
        ListNode node = initLinkedList(a);
        int testMethod = 1;
        boolean result = false;
        switch (testMethod) {
            case 1:
                result = isPalindromeByTwoPoints(node);
                break;
            case 2:
                result = isPalindromeByAllStack(node);
                break;
            case 3:
                result = isPalindromeByHalfStack(node);
                break;
            case 4:
                result = isPalindromeByRe(node);
                break;

        }
        System.out.println("result:" + result);
    }

    private static ListNode initLinkedList(int[] array) {
        ListNode head = null, cur = null;
        for (int i = 0; i < array.length; i++) {
            ListNode newNode = new ListNode(array[i]);
            newNode.next = null;
            if (i == 0) {
                head = newNode;
                cur = head;
            } else {
                cur.next = newNode;
                cur = newNode;
            }
        }
        return head;
    }

    /**
     * 方法1：通过双指针的方式来判断
     *
     * @param head
     * @return
     */
    public static boolean isPalindromeByTwoPoints(ListNode head) {

        return true;
    }

    /**
     * 方法2：全部压栈
     *
     * @param head
     * @return
     */
    public static boolean isPalindromeByAllStack(ListNode head) {

        return true;
    }

    /**
     * 方法3：只将一半的数据压栈
     *
     * @param head
     * @return
     */
    public static boolean isPalindromeByHalfStack(ListNode head) {

        return true;
    }

    /**
     * 方法4：通过递归来实现
     */

    public static boolean isPalindromeByRe(ListNode head) {
        temp = head;
        return check(head);
    }

    private static boolean check(ListNode head) {
        if (head == null)
            return true;
        boolean res = check(head.next) && (temp.val == head.val);
        temp = temp.next;
        return res;
    }

    static class ListNode {
        public int val;
        public ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
